package com.zhongjian.technologycal.mall.activity.web;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.activity.personal.activity.VipLevelActivity;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.utils.LogUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 网页浏览页面实现类
 */
public class WebViewMarkActivity extends BaseActivity {
    @BindView(R.id.web_view)
    WebView mWebView;
    @BindView(R.id.web_progressBar)
    ProgressBar web_progressBar;
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.iv_back_page)
    ImageView iv_back_page;
    String url_host;

    @Override
    protected void initView(Bundle savedInstanceState) {
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);

        url_host = getIntent().getStringExtra("url");
        Log.d("--URL--",url_host);
        mWebView.loadUrl(url_host);
        WebSettings settings = mWebView.getSettings();
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setSupportZoom(true);

        mWebView.setWebChromeClient(new MyWebChrome());
        mWebView.setWebViewClient(new MyWebClient());

    }

    @Override
    protected void beforeInit() {
        super.beforeInit();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_web_view;
    }


    private class MyWebChrome extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            web_progressBar.setProgress(newProgress);
            web_progressBar.setVisibility(View.VISIBLE);

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            tv_main_title.setText(title);
        }
    }


    private class MyWebClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
            LogUtils.logd("截取URL",url);
            view.loadUrl(url);
            if(url.endsWith("key=mykey")){
                Intent intent = new Intent();
                intent.setClass(getBaseContext(), VipLevelActivity.class);
                startActivity(intent);
            }
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            web_progressBar.setVisibility(View.GONE);
        }
    }

    @Override
    public void onBackPressed() {
        mWebView.goBack();
        super.onBackPressed();
    }
    @OnClick(R.id.iv_back_page)
    public void appBackActivity(){
        finish();
    }
    @Override
    public void initPresenter() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mWebView.goBack();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
        }
        return super.onKeyDown(keyCode, event);
    }

}
